package com.suredy.app.porject.model;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.suredy.app.porject.constants.WorkPlanMilestone;
import com.suredy.core.model.BaseModel;
import com.suredy.security.entity.UserEntity;

/**
 * 合同工期计划
 * 
 * @author VIVID.G
 * @since 2015-12-14
 * @version v0.1
 */
@Entity
@Table(name = "tb_contract_work_plan")
public class ContractWorkPlan extends BaseModel {

	private static final long serialVersionUID = 1L;

	/* 里程碑 */
	@Column(name = "milestone", length = 50)
	@Enumerated(EnumType.STRING)
	private WorkPlanMilestone milestone;

	/* 责任人 */
	@Column(name = "header", length = 50)
	private String header;

	/* 计划变更 */
	@Column(name = "changed", columnDefinition = "int default 0")
	private Boolean changed;

	/* 变更原因 */
	@Column(name = "change_reason", columnDefinition = "text")
	private String changeReason;

	/* 主要工作内容 */
	@Column(nullable = false, length = 1000)
	private String work;

	/* 完成情况 */
	@Column(name = "completed", nullable = false, columnDefinition = "int default 0")
	private Boolean completed;

	/* 起始时间节点 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	@Column(name = "start_date_point")
	private Date startDatePoint;

	/* 时间节点 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	@Column(name = "date_point", nullable = false)
	private Date datePoint;

	/* 创建人 */
	@JsonIgnore
	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	@JoinColumn(name = "creater_id")
	private UserEntity creater;

	/* 备注 */
	@Column(length = 3000)
	private String remark;

	/* 关联的招标信息 */
	@JsonIgnore
	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
	@JoinColumn(name = "contract_id")
	private Contract contract;

	public WorkPlanMilestone getMilestone() {
		return milestone;
	}

	public void setMilestone(WorkPlanMilestone milestone) {
		this.milestone = milestone;
	}

	public String getHeader() {
		return header;
	}

	public void setHeader(String header) {
		this.header = header;
	}

	public Boolean getChanged() {
		return changed;
	}

	public void setChanged(Boolean changed) {
		this.changed = changed;
	}

	public String getChangeReason() {
		return changeReason;
	}

	public void setChangeReason(String changeReason) {
		this.changeReason = changeReason;
	}

	public String getWork() {
		return work;
	}

	public void setWork(String work) {
		this.work = work;
	}

	public Boolean getCompleted() {
		return completed;
	}

	public void setCompleted(Boolean completed) {
		this.completed = completed;
	}

	public Date getStartDatePoint() {
		return startDatePoint;
	}

	public void setStartDatePoint(Date startDatePoint) {
		this.startDatePoint = startDatePoint;
	}

	public Date getDatePoint() {
		return datePoint;
	}

	public void setDatePoint(Date datePoint) {
		this.datePoint = datePoint;
	}

	public UserEntity getCreater() {
		return creater;
	}

	public void setCreater(UserEntity creater) {
		this.creater = creater;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Contract getContract() {
		return contract;
	}

	public void setContract(Contract contract) {
		this.contract = contract;
	}

	@Transient
	public boolean isOverDate() {
		if (this.getDatePoint() == null)
			return true;

		return this.getDatePoint().before(new Date());
	}

	@Transient
	public boolean isThisCreator(String userId) {
		if (StringUtils.isBlank(userId))
			return false;

		if (this.getCreater() == null)
			return false;

		return userId.equals(this.getCreater().getId());
	}

}
